package com.carrie.config.handler;

import com.alibaba.fastjson2.JSON;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import java.io.IOException;
import java.util.HashMap;

/**
 * @author XuHaiLong
 * @since 2025/4/15 12:26
 */
public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        String localizedMessage = exception.getLocalizedMessage();
        HashMap<String,Object> map = new HashMap<>();
        map.put("code",-1);
        map.put("msg","登陆失败"+localizedMessage);
        response.setContentType("application/json;charset=utf-8");//响应头
        response.getWriter().println(JSON.toJSONString(map));//写入一个响应结果给前端
    }
}
